package cn.drenal.capcap.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.drenal.capcap.entity.User;
import cn.drenal.capcap.entity.dto.UserDTO;
import cn.drenal.capcap.entity.vo.UserListVO;
import cn.drenal.capcap.exception.BizException;
import cn.drenal.capcap.mapper.UserMapper;
import cn.drenal.capcap.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.secure.SaSecureUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 用户服务实现类
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User createUser(UserDTO userDTO) {
        // 检查用户名是否已存在
        if (lambdaQuery().eq(User::getUsername, userDTO.getUsername()).exists()) {
            throw new BizException("用户名已存在");
        }

        // 创建用户对象
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(SaSecureUtil.md5(userDTO.getPassword()));
        user.setNickname(userDTO.getNickname());
        user.setAvatar(userDTO.getAvatar());
        user.setEmail(userDTO.getEmail());
        user.setPhone(userDTO.getPhone());
        user.setStatus(userDTO.getStatus());

        // 保存用户
        save(user);
        return user;
    }

    @Override
    public User updateUser(Long id, UserDTO userDTO) {
        // 检查用户是否存在
        User user = getById(id);
        if (user == null) {
            throw new BizException("用户不存在");
        }

        // 检查用户名是否被其他用户使用
        if (StringUtils.hasText(userDTO.getUsername())
            && !user.getUsername().equals(userDTO.getUsername())
            && lambdaQuery().eq(User::getUsername, userDTO.getUsername()).exists()) {
            throw new BizException("用户名已存在");
        }

        // 更新用户信息
        if (StringUtils.hasText(userDTO.getUsername())) {
            user.setUsername(userDTO.getUsername());
        }
        if (StringUtils.hasText(userDTO.getNickname())) {
            user.setNickname(userDTO.getNickname());
        }
        if (StringUtils.hasText(userDTO.getAvatar())) {
            user.setAvatar(userDTO.getAvatar());
        }
        if (StringUtils.hasText(userDTO.getEmail())) {
            user.setEmail(userDTO.getEmail());
        }
        if (StringUtils.hasText(userDTO.getPhone())) {
            user.setPhone(userDTO.getPhone());
        }
        if (userDTO.getStatus() != null) {
            user.setStatus(userDTO.getStatus());
        }

        // 保存更新
        updateById(user);

        if (user.getStatus() == 0) {
            StpUtil.logout(user.getId(), "forum");
        }
        return user;
    }

    @Override
    public void deleteUser(Long id) {
        // 检查用户是否存在
        if (!removeById(id)) {
            throw new BizException("用户不存在");
        }
    }

    @Override
    public Page<UserListVO> getUserList(Page<User> page, String username, String nickname, String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(username)) {
            wrapper.like(User::getUsername, username);
        }
        if (StringUtils.hasText(nickname)) {
            wrapper.like(User::getNickname, nickname);
        }
        if (StringUtils.hasText(phone)) {
            wrapper.like(User::getPhone, phone);
        }

        // 执行分页查询
        Page<User> userPage = page(page, wrapper);

        // 转换为UserListVO
        Page<UserListVO> voPage = new Page<>(userPage.getCurrent(), userPage.getSize(), userPage.getTotal());
        voPage.setRecords(userPage.getRecords().stream().map(user -> {
            UserListVO vo = new UserListVO();
            vo.setId(user.getId());
            vo.setUsername(user.getUsername());
            vo.setNickname(user.getNickname());
            vo.setAvatar(user.getAvatar());
            vo.setPhone(user.getPhone());
            vo.setEmail(user.getEmail());
            vo.setStatus(user.getStatus());
            vo.setCreateTime(user.getCreateTime());
            return vo;
        }).collect(java.util.stream.Collectors.toList()));

        return voPage;
    }

    @Override
    public User getUserById(Long id) {
        return getById(id);
    }
}